/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.importer.transform;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.camel.Exchange;
import se.sll.service.catalogue.common.property.CatalogProperties;
import se.sll.service.catalogue.common.rest.model.CodeSystemCode;
import se.sll.service.catalogue.common.rest.request.CreateProductRequest;
import se.sll.service.catalogue.model.vm.Atgard;

/**
 *
 * @author khaleddaham
 */
public class ToProducts {

    /**
     * Convert from Atgard to CreateProductRequest (between vm1.0 and vm2.0
     * datamodel).
     *
     * @param exchange
     * @return
     */
    @SuppressWarnings("unchecked")
    public Exchange process(Exchange exchange) {
        Map map = (Map) exchange.getIn().getBody();
        map.put("product", getProductCreateRequestList((List<Atgard>) exchange.getProperty("products")));
        exchange.getIn().setBody(map);
        return exchange;
    }

    public Exchange setCodes(Exchange exchange) {
        Map map = (Map) exchange.getIn().getBody();
        map.put("codes", getProductCreateRequestList((List<Atgard>) exchange.getProperty("products")));
        exchange.setIn(null);
        exchange.getIn().setBody(map);
        return exchange;
    }

    /**
     *
     * @param productList
     * @return
     */
    public static Iterable<CreateProductRequest> getProductCreateRequestList(List<Atgard> productList) {
        Set<CreateProductRequest> obj = new HashSet<>();
        for (Atgard a : productList) {
            CreateProductRequest req = new CreateProductRequest();
            req.setDescription(a.getForklaring());
            req.setDisplayName(a.getAtgardNamn());
            req.setName(a.getAtgardID());
            req.setUpdatedByHsaid("Import");
            req.setStatus(CreateProductRequest.STATUS.ACTIVE);
            obj.add(req);
        }
        return obj;
    }

    public static Iterable<CodeSystemCode> getCodes(Atgard p) {
        Set<CodeSystemCode> obj = new HashSet<>();
        for (String code : p.getCodes()) {
            CodeSystemCode req = new CodeSystemCode();
            req.setCode(code);
            req.setOid(CatalogProperties.getProperty(CatalogProperties.NAME.CAPACITY_OID));
            obj.add(req);
        }
        return obj;
    }
}
